package com.study.zzedu.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import org.json.JSONObject;

import java.security.MessageDigest;

public class Utils {

    static int second = 0;
    static int minute = 0;
    static int[] iTime = new int[]{0,0,0,0};
    public static String timeToFomart(int time){
        //time++;
        second = time %60;
        minute = time /60;
        if(minute>99){
            return "";
        }
        //Log.e("秒数", ""+second);
        //Log.e("分钟数", ""+minute);
        if(second < 10 && minute < 10){
            iTime[0]=0;
            iTime[1]=minute;
            iTime[2]=0;
            iTime[3]=second;

        }else if(second >= 10 && minute < 10){
            iTime[0]=0;
            iTime[1]=minute;
            iTime[2]=(second+"").charAt(0)-48;
            iTime[3]=(second+"").charAt(1)-48;

        }else if(second < 10 && minute >= 10){
            iTime[0]=(minute+"").charAt(0)-48;
            iTime[1]=(minute+"").charAt(1)-48;
            iTime[2]=0;
            iTime[3]=second;

        }else if(second >= 10 && minute >= 10){
            iTime[0]=(minute+"").charAt(0)-48;
            iTime[1]=(minute+"").charAt(1)-48;
            iTime[2]=(second+"").charAt(0)-48;
            iTime[3]=(second+"").charAt(1)-48;

        }

        return ""+iTime[0]+iTime[1]+":"+iTime[2]+iTime[3];
    }

    public static int getFistNum(String s){
        if(s==null || s.length()==0){
            return -1;
        }
        char c = s.charAt(0);
        if(c>='0' && c<='9'){
            return c-'0';
        }else{
            return getFistNum(s.substring(1));
        }


    }

    public static Bitmap stringToBitmap(String string) {
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    //大小写字母,数字,特殊字符中的至少4种.8位以上,正确返回true
    public static boolean rexCheckPassword(String input) {
        // 8-20 位，字母、数字、字符
        String regStr = "^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[~!@#$%^&*()_+`\\-={}:\";'<>?,.\\/]).{8,10000}$";
        return input.matches(regStr);
    }

    /*对密码进行加密
     *参数：密码
     *返回：密文
     */
    public static String digest(String password) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(password.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                int c = b & 0xff; //负数转换成正数
                String result = Integer.toHexString(c); //把十进制的数转换成十六进制的书
                if(result.length()<2){
                    sb.append(0); //让十六进制全部都是两位数
                }
                sb.append(result);
            }
            return sb.toString(); //返回加密后的密文
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }

    public static String getDefaultStringValue(JSONObject job,String key){
        try {
            return job.getString(key);
        }catch (Exception e){
            return "";
        }
    }

    public static Float getDefaultFloatValue(JSONObject job,String key){
        try {
            return (float)job.getDouble(key);
        }catch (Exception e){
            return (float)0.0;
        }
    }
}
